package cn.lanqiao.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.lanqiao.dao.UserDao;
import cn.lanqiao.entity.Account;
import cn.lanqiao.entity.User;
import cn.lanqiao.util.JdbcUtil;

public class UserDaoImpl implements UserDao{
	@Override
	public User getUser(String UserID) {
		User user = null;
		Connection con = JdbcUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from k_consumer where userid = ?";
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, UserID);
			rs = ps.executeQuery();
			while (rs.next()) {
				String id = rs.getString("userid");
				if (id.equals(UserID)) {
					String U_Level = rs.getString("u_level");
					Integer totalNum = rs.getInt("totalnum");
					Integer winNum = rs.getInt("c_winnum");
					Integer MVPNum = rs.getInt("mvpnum");
					Integer heroNum = rs.getInt("hnum");
					Integer skinNum = rs.getInt("skin");
					String sign = rs.getString("sign");
					Integer FC = rs.getInt("fc");
					String userID = rs.getString("userid");
					user = new User(U_Level, totalNum, winNum, MVPNum, heroNum, skinNum, sign, FC, userID);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {		
			JdbcUtil.close(rs, ps, con);
		}
		if (user != null) {
			return user;
		}
		return user;
	}
	
	@Override
	public Account enterSystem(String UserID) {
		Account account = null;
		Connection con = JdbcUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from k_accountpassword where account = ?";
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, UserID);
			rs = ps.executeQuery();
			while (rs.next()) {
				String id = rs.getString("account");
				if (id.equals(UserID)) {
					String password = rs.getString("password");
					account = new Account(UserID, password);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {		
			JdbcUtil.close(rs, ps, con);
		}		
		if (account != null) {
			return account;
		}
		return account;
	}
}
